<?php
			echo"<script language=\"JavaScript\">
			<!--
			function submitForm(event,formName){
				if(event.keyCode==13 || (event && event.which == 13)){
					confirmDelete(); 
				} 
			}
			//-->
			</script> ";
		
			echo"<form style='padding:0; margin:0;' name=\"form\" action=\"formaction.php\" method=\"post\" enctype=\"multipart/form-data\" onKeyPress=\"submitForm(event,'form');\">";
			echo"<TABLE width=\"100%\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" style=\"mainTableBGColor\">";


//Javascript function to confirm delete
		echo("<script language=\"JavaScript\" type=\"text/javascript\">
		function confirmDelete() {
		var cMsg = \"\";
			for (var q=0; q < document.form.elements.length; q++) {
			
				 // get each element of form form
				 var e = document.form.elements[q];
				 
				 if( e.name.indexOf(\"radioGroup\") == -1 ){
				// not a radioGroup element
				continue;
  				}
				
				 // determine whether delete radio button is checked
				if( e.value.indexOf(\"delete\") == -1 ){
				continue;
				}

				
				if(e.checked == true){
				var i = e.name.replace(/radioGroup/,\"\");
				var h = document.form.elements[\"deleteID\" + i]; 
				cMsg += \"\\t\" + h.value + \"\\n\";
  				}		


					//ends Loop	
					}
					
				if( cMsg != \"\" ){
		
				  // we have some deletes to confirm
				
				  cMsg = \"You are about to delete the following spots:\\n\\n\" + cMsg + \"\\nContinue?\";
				
				  if(confirm(cMsg)){
				 document.form.submit();
				  }else{
				 return false;	
				  }
				
				
				}else {
				document.form.submit();
				}	
				 
				
				//ends function
				}
				</script>");		
		
		
			$myTimeStamp = $_SESSION['timestamp'];
			$TimeStampPlusSevendays = ($myTimeStamp + 518400);

			//#######################################################################################
			//GET SORT METHOD FROM USER INPUT
			//#######################################################################################
			if(!isset($_GET['sortMethod'])){
				$sortMethod = $_SESSION['sortMethod'];
			}else{
				$sortMethod = $_GET['sortMethod'];
				$_SESSION['sortMethod'] = $_GET['sortMethod'];
			}
			
			
			if ($sortMethod == 'client'){//sort by client name alphabeticly
				$sql = "SELECT * FROM $grid_Table WHERE startTimeStamp <= '$TimeStampPlusSevendays' AND endTimeStamp >= '$myTimeStamp' ORDER BY underwriter,mon DESC,tue DESC,wed DESC,thu DESC,fri DESC,sat DESC,sun DESC";
			}else if ($sortMethod == 'single'){//view all entrys for a specific client
				
				if(!$_GET['UWID']){
					$UWID = $_SESSION['UWID'];
				}else{
					$UWID = $_GET['UWID'];
					$_SESSION['UWID'] = $_GET['UWID'];
				}
				
				//$UWID = urldecode($UWID);
				
				$sql = "SELECT underwriter FROM $grid_Table WHERE ID = '$UWID'";
				$resultSort = $db->getAll($sql, DB_FETCHMODE_ASSOC);
				 if (DB::isError($result)) {
				 die ($resultSort->getMessage());
				 } 
				 
				$underwriter= mysql_real_escape_string($resultSort[0]['underwriter']);
				$sql = "SELECT * FROM $grid_Table WHERE startTimeStamp <= '$TimeStampPlusSevendays' AND endTimeStamp >= '$myTimeStamp' AND underwriter = '$underwriter' ORDER BY military,mon DESC,tue DESC,wed DESC,thu DESC,fri DESC,sat DESC,sun DESC";
			}else if ($sortMethod == 'spot'){// view all entries for a specific spot
			
				if(!$_GET['MYID']){
					$MYID = $_SESSION['MYID'];
				}else{
					$MYID = $_GET['MYID'];
					$_SESSION['MYID'] = $_GET['MYID'];
				}
				
				$sql = "SELECT spotID FROM $grid_Table WHERE ID = '$MYID'";
				$resultSort = $db->getAll($sql, DB_FETCHMODE_ASSOC);
				 if (DB::isError($result)) {
				 die ($resultSort->getMessage());
				 }
				 
				$SpotID= $resultSort[0]['spotID'];				
				
				$sql = "SELECT * FROM $grid_Table WHERE startTimeStamp <= '$TimeStampPlusSevendays' AND endTimeStamp >= '$myTimeStamp' AND spotID = '$SpotID' ORDER BY military,mon DESC,tue DESC,wed DESC,thu DESC,fri DESC,sat DESC,sun DESC";
			}else if ($sortMethod == 'allSpots'){// Sort entries by Spot name alphabeticle
				
				$sql = "SELECT * FROM $grid_Table WHERE startTimeStamp <= '$TimeStampPlusSevendays' AND endTimeStamp >= '$myTimeStamp' ORDER BY spotID,mon DESC,tue DESC,wed DESC,thu DESC,fri DESC,sat DESC,sun DESC";
			}else{//default sort method by time
				$sql = "SELECT * FROM $grid_Table WHERE startTimeStamp <= '$TimeStampPlusSevendays' AND endTimeStamp >= '$myTimeStamp' ORDER BY military,mon DESC,tue DESC,wed DESC,thu DESC,fri DESC,sat DESC,sun DESC";
			}
			
			
			$result = $db->getAll($sql, DB_FETCHMODE_ASSOC);
				 if (DB::isError($result)) {
				 die ($result->getMessage());
				 }
				 
				 if(count($result) == 0){//No results will show cuz everything has been deleted ot the likes so  create view by time
					 $sql = "SELECT * FROM $grid_Table WHERE startTimeStamp <= '$TimeStampPlusSevendays' AND endTimeStamp >= '$myTimeStamp' ORDER BY military";
					 $result = $db->getAll($sql, DB_FETCHMODE_ASSOC);
					 if (DB::isError($result)) {
					 die ($result->getMessage());
					 }
					 
					 $sortMethod = 'time';
					 unset($_SESSION['sortMethod']);
					 unset($_GET['sortMethod']);
				 }
				 
				$_SESSION['LiveGridPrint'] = array(); 
				$_SESSION['LiveGridPrint'] = $result;
				
	 

			//++++++++++++++++++++++++++++++++++++
			//BEGIN GET DATA AND CREATEVIEW
			//++++++++++++++++++++++++++++++++++++
			
			echo ("<TR><TD align=\"center\">\n");
			echo("<TABLE width=\"100%\" border=\"1\" cellpadding=\"1\" cellspacing=\"0\">\n\n");
			echo("<TR class=\"blockHeader\"><TD width=\"40\">Time</TD> <TD>Client Name</TD> <TD>Spot</TD> <TD>Days</TD> <TD>Rate</TD> <TD>Action</TD></TR> \n");
			
			//ARRAY TO SEE IF THERE ARE DUPLICATE TIME ENTRIES
			$lastTime = array();
			$weeklyTotal = array();
			$mondayTotal = array();
			$tuesdayTotal = array();
			$wednesdayTotal = array();
			$thursdayTotal = array();
			$fridayTotal = array();
			$saturdayTotal = array();
			$sundayTotal = array();

			
			
			
			$i = 1;
			$z = 0;
			while ($z < count($result)) {
			$ID = $result[$z]['ID'];
			$underwriter = stripslashes($result[$z]['underwriter']);
			$abbreviation = stripslashes($result[$z]['abbreviation']);
			$startTimeStamp = $result[$z]['startTimeStamp'];
			$endTimeStamp = $result[$z]['endTimeStamp'];
			$length = $result[$z]['length'];
			$spotID = $result[$z]['spotID'];
			$hour = $result[$z]['hour'];
			$minute = $result[$z]['minute'];
			$seconds = $result[$z]['seconds'];
			$ampm = $result[$z]['ampm'];
			$military = $result[$z]['military'];
			$mon = $result[$z]['mon'];
			$tue = $result[$z]['tue'];
			$wed = $result[$z]['wed'];
			$thu = $result[$z]['thu'];
			$fri = $result[$z]['fri'];
			$sat = $result[$z]['sat'];
			$sun = $result[$z]['sun'];
			$type = $result[$z]['type'];
			$rate = $result[$z]['rate'];
			$live = $result[$z]['live'];
			$trade = $result[$z]['trade'];
			echo("\n<input name=\"deleteID$i\" type=\"hidden\" value=\"$spotID  -  $hour:$minute:$seconds $ampm\"> \n");
			
			if ($sortMethod == 'time' || !isset($sortMethod)){//
				$lastTime[] = $military;
			}
			

			if($i & 1){
			$myBGColor = "#FFFFFF";
			echo("<TR onmouseover=\"setPointer(this, $i, 'over', '#FFFFFF', '#CCFFCC', '#FFCC99');\" onmouseout=\"setPointer(this, $i, 'out', '#FFFFFF', '#CCFFCC', '#FFCC99');\" onmousedown=\"setPointer(this, $i, 'click', '#FFFFFF', '#CCFFCC', '#FFCC99');\">\n  <TD bgcolor=\"#FFFFFF\" class=\"TimeColumn\" nowrap>\n");
			}else{
			$myBGColor = "#CCCCCC";
			echo("<TR onmouseover=\"setPointer(this, $i, 'over', '#CCCCCC', '#CCFFCC', '#FFCC99');\" onmouseout=\"setPointer(this, $i, 'out', '#CCCCCC', '#CCFFCC', '#FFCC99');\" onmousedown=\"setPointer(this, $i, 'click', '#CCCCCC', '#CCFFCC', '#FFCC99');\">\n  <TD bgcolor=\"#CCCCCC\" class=\"TimeColumn\" nowrap>\n");
			}
				if($military != $lastTime[$i-2]){
				echo ("$hour:$minute:$seconds $ampm</TD>\n  ");
				}else{
				echo "&nbsp;</TD>\n ";
				}
				echo("<TD bgcolor=\"$myBGColor\" class=\"SpotLink\"><A HREF=\"index.php?ID=0&amp;subMenu=2&amp;action=view&amp;TS=".$_SESSION['timestamp']."&amp;sortMethod=single&amp;UWID=$ID\">$underwriter</A></TD> \n");
				echo("<TD bgcolor=\"$myBGColor\" class=\"SpotLink\"> \n");
				echo "<A HREF=\"index.php?ID=0&amp;subMenu=2&amp;action=view&amp;TS=".$_SESSION['timestamp']."&amp;sortMethod=spot&amp;MYID=$ID\">". $spotID . "</A>\n";
				echo("</TD>\n");
				echo("<TD bgcolor=\"$myBGColor\" class=\"formElementTextLeft\">\n");
				$multiplyer = 0;
				
				if($mon == 'true' && ($myTimeStamp + 0) <= $endTimeStamp){
				echo "Mon&nbsp;" . date("m/d",$_SESSION['timestamp']). "<BR>";
				$multiplyer = ($multiplyer + 1);
				$mondayTotal[] = 1;
				}else if($mon == 'true' && $endTimeStamp == 'NA'){
				echo "Mon&nbsp;" . date("m/d",$_SESSION['timestamp']) . "<BR>";
				$multiplyer = ($multiplyer + 1);
				$mondayTotal[] = 1;
				}
				
				if($tue == 'true' && ($myTimeStamp + (86400 * 1)) <= $endTimeStamp){
				echo "Tue&nbsp;" . date("m/d",($_SESSION['timestamp'] + 86400)) . "<BR>";
				$multiplyer = ($multiplyer + 1);
				$tuesdayTotal[] = 1;
				}else if($tue == 'true' && $endTimeStamp == 'NA'){
				echo "Tue&nbsp;" . date("m/d",($_SESSION['timestamp'] + 86400)) . "<BR>";
				$multiplyer = ($multiplyer + 1);
				$tuesdayTotal[] = 1;
				}
				
				if($wed == 'true' && ($myTimeStamp + (86400 * 2)) <= $endTimeStamp){
				echo "Wed&nbsp;" . date("m/d",($_SESSION['timestamp'] + (86400*2))) . "<BR>";
				$multiplyer = ($multiplyer + 1);
				$wednesdayTotal[] = 1;
				}else if($wed == 'true' && $endTimeStamp == 'NA'){
				echo "Wed&nbsp;" . date("m/d",($_SESSION['timestamp'] + (86400*2))) . "<BR>";
				$multiplyer = ($multiplyer + 1);
				$wednesdayTotal[] = 1;
				}
				
				if($thu == 'true' && ($myTimeStamp + (86400 * 3)) <= $endTimeStamp){
				echo "Thu&nbsp;" . date("m/d",($_SESSION['timestamp'] + (86400*3))) . "<BR>";
				$multiplyer = ($multiplyer + 1);
				$thursdayTotal[] = 1;
				}else if($thu == 'true' && $endTimeStamp == 'NA'){
				echo "Thu&nbsp;" . date("m/d",($_SESSION['timestamp'] + (86400*3))) . "<BR>";
				$multiplyer = ($multiplyer + 1);
				$thursdayTotal[] = 1;
				}
				
				if($fri == 'true' && ($myTimeStamp + (86400 * 4)) <= $endTimeStamp){
				echo "Fri&nbsp;" . date("m/d",($_SESSION['timestamp'] + (86400*4))) . "<BR>";
				$multiplyer = ($multiplyer + 1);
				$fridayTotal[] = 1;
				}else if($fri == 'true' && $endTimeStamp == 'NA'){
				echo "Fri&nbsp;" . date("m/d",($_SESSION['timestamp'] + (86400*4))) . "<BR>";
				$multiplyer = ($multiplyer + 1);
				$fridayTotal[] = 1;
				}
				
				if($sat == 'true' && ($myTimeStamp + (86400 * 5)) <= $endTimeStamp){
				echo "Sat&nbsp;" . date("m/d",($_SESSION['timestamp'] + (86400*5))) . "<BR>";
				$multiplyer = ($multiplyer + 1);
				$saturdayTotal[] = 1;
				}else if($sat == 'true' && $endTimeStamp == 'NA'){
				echo "Sat&nbsp;" . date("m/d",($_SESSION['timestamp'] + (86400*5))) . "<BR>";
				$multiplyer = ($multiplyer + 1);
				$saturdayTotal[] = 1;
				}
				
				if($sun == 'true' && ($myTimeStamp + (86400 * 6)) <= $endTimeStamp){
				echo "Sun&nbsp;" . date("m/d",($_SESSION['timestamp'] + (86400*6))) . "<BR>";
				$multiplyer = ($multiplyer + 1);
				$sundayTotal[] = 1;
				}else if($sun == 'true' && $endTimeStamp == 'NA'){
				echo "Sun&nbsp;" . date("m/d",($_SESSION['timestamp'] + (86400*6))) . "<BR>";
				$multiplyer = ($multiplyer + 1);
				$sundayTotal[] = 1;
				}

				echo("</TD>\n");				 
				
				if($trade == 'true'){
				echo("<TD bgcolor=\"$myBGColor\" class=\"timeListing\" style=\"text-transform:capitalize; \"> \n");
				echo "TRADE";
				}else{
				echo("<TD bgcolor=\"$myBGColor\" class=\"formElementTextLeft\"> \n");
				echo number_format($rate, 2, '.', ',');
				}
				
				
				echo("</TD>\n");
				if($trade == 'true'){
				}else{		
				$weeklyTotal[] = ($multiplyer * $rate);	
				}		
				echo"</TD>\n"; 
				echo(" <TD bgcolor=\"$myBGColor\" align=\"left\" valign=\"top\" >\n");
				echo("\n<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"formElementTextLeft\">");
				echo("\n<TR><TD valign=\"top\" align=\"left\"><INPUT name=\"radioGroup$i\" type=\"radio\" value=\"update:$ID\" ></TD><TD valign=\"middle\" align=\"left\">update</TD></TR>");
				echo("\n<TR><TD valign=\"top\" align=\"left\"><INPUT name=\"radioGroup$i\" type=\"radio\" value=\"delete:$ID\" ></TD> <TD valign=\"middle\" align=\"left\">delete</TD></TR>");
				echo("\n</TABLE>");
				echo("\n\n</TD></TR>");
			

			++$i;
			++$z;
			}
			
			echo("\n</TABLE>");
			echo("\n</TD></TR>");
			//++++++++++++++++++++++++++++++++++++
			//END GET DATA AND CREATE DEFAULT GRID VIEW
			//++++++++++++++++++++++++++++++++++++
			
			
		echo("\n\n<INPUT name=\"highnumber\" type=\"hidden\" value=\"$i\">");
		echo("<TR><TD valign=\"middle\" align=\"center\" bgcolor=\"#666666\"><TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><TR><TD valign=\"middle\" align=\"center\" width=\"33%\"><FONT color=\"#FFFFFF\" size=\"-1\"><B>Number of Spots:</B></FONT><BR>");
		echo("<TABLE width=\"100%\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\">");
		echo("<TR bgcolor=\"#CCCCCC\"><TD valign=\"top\" align=\"center\"><FONT color=\"#333333\" size=\"-1\">M</FONT></TD> <TD valign=\"top\" align=\"center\"><FONT color=\"#333333\" size=\"-1\">T</FONT></TD> <TD valign=\"top\" align=\"center\"><FONT color=\"#333333\" size=\"-1\">W</FONT></TD> <TD valign=\"top\" align=\"center\"><FONT color=\"#333333\" size=\"-1\">Th</FONT></TD> <TD valign=\"top\" align=\"center\"><FONT color=\"#333333\" size=\"-1\">F</FONT></TD> <TD valign=\"top\" align=\"center\"><FONT color=\"#333333\" size=\"-1\">S</FONT></TD> <TD valign=\"top\" align=\"center\"><FONT color=\"#333333\" size=\"-1\">Su</FONT></TD></TR>");
		echo("<TR ><TD valign=\"top\" align=\"center\"><FONT color=\"#FFFFFF\" size=\"-1\">" . count($mondayTotal) . "</FONT></TD> <TD valign=\"top\" align=\"center\"><FONT color=\"#FFFFFF\" size=\"-1\">" . count($tuesdayTotal) . "</FONT></TD> <TD valign=\"top\" align=\"center\"><FONT color=\"#FFFFFF\" size=\"-1\">" . count($wednesdayTotal) . "</FONT></TD> <TD valign=\"top\" align=\"center\"><FONT color=\"#FFFFFF\" size=\"-1\">" . count($thursdayTotal) . "</FONT></TD> <TD valign=\"top\" align=\"center\"><FONT color=\"#FFFFFF\" size=\"-1\">" . count($fridayTotal) . "</FONT></TD> <TD valign=\"top\" align=\"center\"><FONT color=\"#FFFFFF\" size=\"-1\">" . count($saturdayTotal) . "</FONT></TD> <TD valign=\"top\" align=\"center\"><FONT color=\"#FFFFFF\" size=\"-1\">" . count($sundayTotal) . "</FONT></TD></TR>");
		echo("</TABLE>");
		echo ("</td> <TD valign=\"middle\" align=\"center\" width=\"33%\"><INPUT name=\"maintainViewStamp\" type=\"hidden\" value=\"" . $_SESSION['timestamp'] . "\"><INPUT name=\"action\" type=\"hidden\" value=\"modifyGrid\"><INPUT name=\"Submit\" type=\"button\" class=\"inputButton\"  value=\"Submit\" onClick=\"confirmDelete()\"> &nbsp; &nbsp; &nbsp; <INPUT name=\"Reset\" type=\"reset\" class=\"inputButton\" value=\"Reset\"></TD> <TD valign=\"middle\" align=\"center\" width=\"33%\"><FONT color=\"#FFFFFF\" size=\"-1\"><B><U>Weekly Revenue: $" . number_format((array_sum($weeklyTotal)), 2, '.', ',') . "</U></B></FONT></TD></TR>");
		echo("</TABLE></FORM>");		
		
		

?>